package edu.cmu.cs.webapp.ioms.ui.wizards.user;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import edu.cmu.cs.webapp.ioms.dao.DataAccessException;
import edu.cmu.cs.webapp.ioms.dao.impl.UserDAOImpl;
import edu.cmu.cs.webapp.ioms.models.PurchaseOrder;
import edu.cmu.cs.webapp.ioms.models.User;
import edu.cmu.cs.webapp.ioms.utils.IOMSConstants;

/**
 * @author Christian
 * 
 */
public class UserWizardPage extends WizardPage {

	private Text loginTxt;
	private Text initPwdTxt;

	private Text nameTxt;
	private Combo roleComb;
	private Text departmentTxt;

	public UserWizardPage(String pageName) {
		super(pageName);
		setTitle("Add new user");
		setDescription("Input the necessary information of the new user. \nNotice: All fields with asterisk(*) are required");

	}

	public UserWizardPage(String pageName, String title,
			ImageDescriptor titleImage) {
		super(pageName, title, titleImage);
	}

	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		container.setLayout(layout);

		layout.numColumns = 3;
		// layout.verticalSpacing = 9;

		Label label = new Label(container, SWT.NULL);

		/* id */
		label.setText("Login*:"); //$NON-NLS-1$

		loginTxt = new Text(container, SWT.BORDER | SWT.SINGLE);

		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;
		// _memberIDTxt.setEditable(false);
		loginTxt.setLayoutData(gd);
		loginTxt.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				validate();
			}
		});

		// initial password
		label = new Label(container, SWT.NULL);
		label.setText("Initial Password*:"); //$NON-NLS-1$
		initPwdTxt = new Text(container, SWT.BORDER | SWT.PASSWORD);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;
		initPwdTxt.setLayoutData(gd);
		initPwdTxt.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				validate();
			}
		});

		createLine(container, layout.numColumns);

		// name
		label = new Label(container, SWT.NULL);
		label.setText("Name*:"); //$NON-NLS-1$
		nameTxt = new Text(container, SWT.BORDER);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;
		nameTxt.setLayoutData(gd);
		nameTxt.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				validate();
			}
		});

		// role
		label = new Label(container, SWT.NULL);
		label.setText("Role*:"); //$NON-NLS-1$
		roleComb = new Combo(container, SWT.BORDER);
		roleComb.setItems(IOMSConstants.USER_ROLES);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;
		roleComb.setLayoutData(gd);
		roleComb.select(0);

		// department
		label = new Label(container, SWT.NULL);
		label.setText("Department:"); //$NON-NLS-1$

		departmentTxt = new Text(container, SWT.BORDER | SWT.SINGLE);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;
		departmentTxt.setLayoutData(gd);

		if (isUpdate()) {
			loadFromExistingModel();
		}

		setControl(container);
		validate();
		container.pack(true);

	}

	private void loadFromExistingModel() {
		User model = getModel();
		if (model != null) {
			this.loginTxt.setText(model.getLogin());
			this.loginTxt.setEnabled(false);
			this.initPwdTxt.setText(model.getPassword() == null ? "" : model
					.getPassword());
			this.nameTxt.setText(model.getName());
			this.roleComb.setText(model.getRole() == null ? "" : model
					.getRole());
			this.departmentTxt.setText(model.getDepartment() == null ? ""
					: model.getDepartment());
		}

	}

	private void createLine(Composite parent, int ncol) {
		Label line = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL
				| SWT.BOLD);
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.horizontalSpan = ncol;
		line.setLayoutData(gridData);
	}

	private boolean validate() {
		boolean valid = true;
		String errorMessage = null;
		// if (valid && (getOrderID() == null || getOrderID().length() == 0)) {
		// valid = false;
		// errorMessage = "Member id must be specified";
		// }

		if (valid && (getLogin() == null || getLogin().length() == 0)) {
			valid = false;
			errorMessage = "Login field could not be empty";
		}

		if (!isUpdate()) {
			User findUser = null;

			try {
				findUser = new UserDAOImpl().findUser(getLogin());
			} catch (DataAccessException e) {
			}
			if (valid && findUser != null) {
				valid = false;
				errorMessage = "Login name already exist";
			}
		}

		if (valid
				&& (getInitialPassword() == null || getInitialPassword()
						.length() == 0)) {
			valid = false;
			errorMessage = "Password field could not be empty";

		}

		if (valid && (getName() == null || getName().length() == 0)) {
			valid = false;
			errorMessage = "Name field must be specified";
		}

		if (valid) {
			setErrorMessage(null);
			setPageComplete(true);
		} else {
			setErrorMessage(errorMessage);
			setPageComplete(false);
		}

		return valid;

	}

	public String getLogin() {
		return loginTxt.getText();
	}

	public String getName() {
		return nameTxt.getText();
	}

	public String getRole() {
		return roleComb.getText();
	}

	public String getInitialPassword() {
		return initPwdTxt.getText();
	}

	public String getDepartment() {
		return departmentTxt.getText();
	}

	private boolean isUpdate() {
		return ((UserWizard) getWizard()).isUpdate();
	}

	private User getModel() {
		return ((UserWizard) getWizard()).getModel();
	}

}
